package com.argticling.service;

import java.io.File;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.argticling.model.Article;
import com.sun.syndication.feed.synd.*;
import com.sun.syndication.io.FeedException;
import com.sun.syndication.io.SyndFeedOutput;


public class FeedBuilderService {
    private ArticleService articleService;
    //Deberia ser un dato que podemos obtener del feed, para saber el tipo de dato soportado
    public static final String DESCRIPTION_TYPE = "text/plain";
    public static final String FEED_TYPE = "rss_2.0";

    public String getFeedXML(List<Article> listArticle) throws FeedException{
        //todo ver de hacer un metodo que pida al ArticleService en base al String
        //investigar un modo de agregar secciones de forma dinamica, ej.: dao.getLast(cantidad, seccion);
        //      List articles = articleService.getLastArticles(cantidad, seccion);
        //      SyndFeed feed = generateFeed(articles);
        System.out.println(""+listArticle);
        if(listArticle==null){
            SyndFeed feed = feedTest();
            System.out.println(""+serialize(feed));
            return serialize(feed);
        }        
        return serialize(generateFeed(listArticle));
    }

    private SyndFeed generateFeed(List<Article> listArticle) {
        SyndFeed feed = new SyndFeedImpl();//todo ver de optimizar manteniendo en memoria p evitar procesamiento innecesario
        feed.setFeedType(FEED_TYPE);
        List entries = feed.getEntries();
        if (entries == null) {
            entries = new ArrayList();
        }
        for (Article article : listArticle) {
            //Todo Pendiente LINK
            entries.add(buildEntry(article.getDc().getTitle(), "", article.getDc().getDate(), article.getDc().getDescription()));
        }
        feed.setEntries(entries);
        return feed;
    }

    private SyndEntry buildEntry(String title, String link, String date, String text) {
        Date parsedDate = new Date();

        SyndEntry entry = new SyndEntryImpl();
        entry.setTitle(title);
        entry.setLink(link);
        entry.setPublishedDate(parsedDate);

        SyndContent description = new SyndContentImpl();
        description.setType(DESCRIPTION_TYPE);
        description.setValue(text);

        entry.setDescription(description);

        return entry;
    }

    private String serialize(SyndFeed feed) throws FeedException {
        SyndFeedOutput output = new SyndFeedOutput();
        return output.outputString(feed);
    }

    public void setArticleService(ArticleService articleService) {
        this.articleService = articleService;
    }

    public ArticleService getArticleService() {
        return this.articleService;
    }

    //todo **   TESTING SECTION   **
    //Creo un feed de pruebas
    private SyndFeed feedTest() {
        SyndFeed feed = new SyndFeedImpl();
        feed.setFeedType(FEED_TYPE);
        //Sets de prueba
        feed.setTitle("LAB II");
        feed.setLink("www.google.com.ar");
        feed.setDescription("Pruebas");
        return feed;
    }
}